#include <avr/io.h>
#include <avr/pgmspace.h>
#define F_CPU 1000000UL
#include <util/delay.h>

//main library used
#include "nokia5110.h"
#include "main.h"

void initADC(void)
{
	//PC0(ADC0) should be 0(INPUT)
	DDRC = 0x00;
	ADCSRA |= (1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);   //make ADC enable and select clk/128
	ADMUX |= (1<<REFS0)|(1<<ADLAR); // ADC Channel 0 Selected, data will be left justified with reference voltage same as VCC
}

uint16_t readADC3(void)
{
	ADCSRA |= (1<<ADSC);                    //start conversion
	while((ADCSRA&(1<<ADIF))==0);   //wait for end of conversion
	return ADC;
}
int main()
{
	//initialize  LCD
    LcdInit();
	//initialize A2D conversion
	initADC();

    //display dark image (testing)
    LcdContrast(0x7F);
	_delay_ms(1200);
    LcdClear();

	//adjust proper contrast
    LcdContrast(0x3F);
    _delay_ms(1200);
    LcdClear();
	
	//custom please wait image
    LcdImage(waitImage); 
    LcdUpdate();
    _delay_ms(5000);

	while (1)
	{
		//read analog temp
		uint16_t tempval = readADC3();
		uint16_t Dig1 = tempval%10;
		uint16_t Dig2 = tempval%100;
		uint16_t Dig3 = tempval%1000;
		uint16_t Dig4 = tempval%10000;
		char buffer[5];
		sprintf(buffer, "%d%d.%d%d", Dig1, Dig2, Dig3, Dig4);
		//display string
		LcdClear();
		LcdGotoXYFont(1,1);
		LcdFStr(FONT_1X,buffer);
		LcdGotoXYFont(1,2);
		LcdFStr(FONT_1X,(unsigned char*)PSTR("Fahrenheit"));
		LcdUpdate();
		_delay_ms(1000);
	}
    return 0;
}
